<?php
include_once 'cControllerCore.php';
/**
 * Description of cCmsController
 *
 * @author Faceless
 */
class cCmsController {
    //put your code here
    private $cmsBaseDir;
    public function __construct() {
        $cmsBaseDir = realpath(basedir(__FILE__).'../../');
    }

    public function getCmsBaseDir() {
        return $this->cmsBaseDir;
    }

    public function registerObject($objName, $objType){
        $objTypes = array (
            'controller' => array(
                'dir' => 'core/controllers/',
                'prefix' => 'c',
                'needInstance' => true
            ),
            'interface' => array(
                'dir' => 'core/interfaces/',
                'prefix' => 'i',
                'needInstance' => false
            ),
            'model' => array(
                'dir' => 'core/models/',
                'prefix' => 'm',
                'needInstance' => true
            ),
            'component' => array(
                'dir' => "components/$objName/",
                'prefix' => '',
                'needInstance' => true
            )
        );

        $objTypeResolved = $objTypes[$objType];
        if (!$objTypeResolved){
            return false;
        }
        else {
            $objFile = "{$this->cmsBaseDir}{$objTypeResolved['dir']}{$objTypeResolved['prefix']}{$objName}.php";
        }

        include_once $objFile;

        if ($objTypeResolved['needInstance']){
            $objInstance = new ${$objTypeResolved['prefix']}{$objname}();
            return $objInstance;
        }
        else {
            return null;
        }
    }

    public function registerModel($modelName) {
        return $this->registerObject($modelName, 'model');
    }

    public function registerController($controllerName) {
        return $this->registerObject($controllerName, 'controller');
    }

    public function registerInterface($interfaceName) {
        return $this->registerObject($interfaceName, 'interface');
    }

    public function registerComponent($componentName) {
        return $this->registerObject($componentName, 'component');
    }
}
?>
